package com.cyan.app.workflow.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cyan.app.workflow.R;

/**
 * Created by DXW on 2015/12/16.
 */
public class LoadingDialog {
    Dialog dialog;
    TextView loading_tv_progress;
    ProgressBar loading_pb_bar;
    TextView loading_tv_tip;
    LinearLayout loading_ll_progress;

    int pb_width = 0;
    int ll_left = 0;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    public LoadingDialog(Context context) {
        dialog = createDialog(context);
    }

    private Dialog createDialog(Context context) {
        final Dialog loadingDialog = new Dialog(context, R.style.LoadingProgressDialog);

        loadingDialog.setCancelable(false);
        loadingDialog.setCanceledOnTouchOutside(false);

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);

        loadingDialog.setContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        loading_ll_progress = (LinearLayout) v.findViewById(R.id.loading_ll_progress);
        loading_tv_progress = (TextView) v.findViewById(R.id.loading_tv_progress);
        loading_pb_bar = (ProgressBar) v.findViewById(R.id.loading_pb_bar);
        loading_tv_tip = (TextView) v.findViewById(R.id.loading_tv_tip);

        loading_pb_bar.setMax(100);
        loading_pb_bar.setProgress(0);

        return loadingDialog;
    }

    public void show() {
        dialog.show();
    }

    public void cancel() {
        dialog.cancel();
    }

    public void setProgress(int progress) {
        loading_pb_bar.setProgress(progress);
        handler.post(new Runnable() {
            @Override
            public void run() {
                loading_tv_progress.setText((loading_pb_bar.getProgress() * 100 / loading_pb_bar.getMax()) + "%");

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) loading_ll_progress.getLayoutParams();
                if(pb_width <= 0) {
                    pb_width =  loading_pb_bar.getWidth();
                }

                if(ll_left <= 0) {
                    ll_left = lp.leftMargin;
                }

                int pos = pb_width * (loading_pb_bar.getProgress() * 100 / loading_pb_bar.getMax()) / 100;
                lp.setMargins(ll_left + pos, lp.topMargin, lp.rightMargin, lp.bottomMargin);
                loading_ll_progress.setLayoutParams(lp);
            }
        });
    }

    public int getProgress() {
        return loading_pb_bar.getProgress();
    }

    public void setMax(int max) {
        if(max > 0)
            loading_pb_bar.setMax(max);
    }

    public int getMax() {
        return loading_pb_bar.getMax();
    }

    public Dialog getDialog() {
        return dialog;
    }
}
